<?php

// $Id$


require_once($WSC->get_site_inc_dir().'modules/admin_panel/site_admin_web_page_wed_ua.class.php');


class User_Enable_WP extends IT_Admin_Web_Page_WED_UA {
	public function init() {
		parent::init();

		$v = $this->create_view();
		$this->add_view($v);

		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('enable', Tangra_Parameter_Method::GET, 'enable'), 'enable_event');
		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('enable_confirmed', Tangra_Parameter_Method::GET, 'enable_confirmed'), 'enable_confirmed_event');

	}


	protected function enable_event($id) {
		$user = new User_DBC();
		$dbc = $this->get_dbc();
		if ($user->load_by_id($dbc, $id)) {

			$this->export('username', $user->get_username());
			$this->export('user_id', $user->get_id());

			$view = $this->get_view('default');

		} else {
			$view = $this->get_view('not_exists');

		}


		return $view;
	}

//TODO - confirm that it is working
	protected function enable_confirmed_event($id) {
		$dbc = $this->get_dbc();

		try {
			$dbc->start_trans();
			User_DBC::enable($dbc, $id);
			$dbc->complete_trans();
		} catch (Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			throw $e;
		}

		$context = $this->get__context();
		$rc = new Redirect_Composer_Local($this->get__context(), 'admin/user/users.php');
		$redir_view = new Just_Headers_View();
		$redir_view->add_http_header($rc->get_location($context));

		return $redir_view;
	}


	protected function default_event() {
		throw new Tangra_User_Exception('Unexpected parameters.');
	}
}

